#pragma strict
//////////////////////////////////////////////////////
///分辨率 设置
//////////////////////////////////////////////////////
var resRect:Rect = Rect(210,10,200,30);
var defaultRect:Rect = Rect(10,10,40,30);
var optionsRect:Rect;
var buttonHeight:float = 20;

var isShowOptions:boolean;
var isFullScreen:boolean;
var isShowFog:boolean;
var maxFps:float = 300;

static var STR_OPTION_ON:String = "选项开启";
static var STR_OPTION_OFF:String  = "选项关闭";
static var STR_FULLSCREEN:String = "全屏";
static var STR_FOG:String = "雾";

private var selectedResIndex:int;
private var curResIndex:int;

private var optionScrollPos:Vector2;
private var grayscaleEffect:GrayscaleEffect;

/** 雾设置 */
var fogValue:float = 0.0;
var leftFogValue:float = 0.0;
var rightFogValue:float = 0.18;

function Start(){
	optionsRect = Rect(defaultRect.xMin,defaultRect.yMax,120,Screen.height - defaultRect.yMax);
	Application.targetFrameRate = maxFps;
	grayscaleEffect = GetComponent.<GrayscaleEffect>();
}
function LateUpdate(){
	ChangeResolution();
	ChangeFog();
}
function OnGUI(){
	GUI.Label(resRect,"w:"+Screen.currentResolution.width+",h:"+Screen.currentResolution.height+",rate:"+Screen.currentResolution.refreshRate);
	isShowOptions = GUI.Toggle(defaultRect,isShowOptions,isShowOptions?STR_OPTION_OFF:STR_OPTION_ON);
	if(isShowOptions){
		GUILayout.BeginArea(optionsRect,"","box");
			ShowOptions();
			ShowFullScreen();
			ShowFogOptions();
		GUILayout.EndArea();
	}
}
/** 分辨率选项 */
function ShowOptions(){
	optionScrollPos = GUILayout.BeginScrollView(optionScrollPos,GUILayout.Height(6 * buttonHeight));
		for(var res:Resolution in Screen.resolutions){
			if(GUILayout.Button(res.width+":"+res.height,GUILayout.Height(buttonHeight))){
				selectedResIndex = System.Array.IndexOf(Screen.resolutions,res);
			}
		}
	GUILayout.EndScrollView();
}
function ShowFullScreen(){
	isFullScreen = GUILayout.Toggle(isFullScreen,STR_FULLSCREEN);
	if(isFullScreen){
		Screen.fullScreen = isFullScreen;
	}
}
/** fog */
function ShowFogOptions(){
	isShowFog = GUILayout.Toggle(isShowFog,STR_FOG);
	if(isShowFog){
		fogValue = GUILayout.HorizontalSlider(fogValue,leftFogValue,rightFogValue);
	}
}
/** 分辨率 */
function ChangeResolution(){
	if(curResIndex != selectedResIndex){
		curResIndex = selectedResIndex;
		var res:Resolution = Screen.resolutions[curResIndex];
		Screen.SetResolution(res.width,res.height,isFullScreen,res.refreshRate);
	}
}
/** fog */
function ChangeFog(){
	if(RenderSettings.fog != isShowFog || RenderSettings.fogDensity != fogValue){
		RenderSettings.fog = isShowFog;
		RenderSettings.fogDensity = fogValue;
	}
}
/** 显示灰度 */
function OnShowGray(){
	if(grayscaleEffect){
		grayscaleEffect.enabled = true;
	}
}
function OnUnshowGray(){
	if(grayscaleEffect)
		grayscaleEffect.enabled = false;
}